#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
enum COLOR
{
    WHITE,
    GRAY,
    BLACK
};
struct MyNode
{
    TreeNode *tnode;
    int color;
    MyNode(TreeNode *tnode = nullptr, int color = WHITE) : tnode(tnode), color(color){};
};
class Solution
{
public:
    int minval = INT32_MAX, _hot = -1;
    stack<MyNode> stk;
    int getMinimumDifference(TreeNode *root)
    {
        stk.emplace(root, WHITE);
        while (stk.empty() == false)
        {
            MyNode nd = stk.top();
            stk.pop();
            if (nd.tnode == nullptr)
                continue;
            if (nd.color == GRAY)
            {
                if (_hot != -1)
                    minval = min(minval, nd.tnode->val - _hot);
                _hot = nd.tnode->val;
            }
            else
            {
                stk.emplace(nd.tnode->right, WHITE);
                stk.emplace(nd.tnode, GRAY);
                stk.emplace(nd.tnode->left, WHITE);
            }
        }
        return minval;
    }
};